﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections;


namespace WindowsFormsApplication3.Invoice
{
    using Reports;
    using Reports.JobDetailsTableAdapters;

    public partial class JobCodeCompletionForm : Form
    {
        Transaction.InvoiceTransaction invtrnsctn = null;
        public JobCodeCompletionForm()
        {
            invtrnsctn = new Transaction.InvoiceTransaction();
            InitializeComponent();
        }

        private void JobCodeCompletionForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'jobDetails.TblJobCode' table. You can move, or remove it, as needed.

            jobcomboboxload();
        }

       
        public void jobcomboboxload()
        {

            JobCodeTableAdapter adapt = new JobCodeTableAdapter();
            adapt.Connection = new OleDbConnection(Program.ConnStr);//This is not mandatory.just to confirm.
            DataTable dt = adapt.loaduncompletedjobcode();
            cmbjobcode.ValueMember = "jobpk";
            cmbjobcode.DisplayMember = "jobcode";
            cmbjobcode.DataSource = dt;
        }





        public void selectedjobcodedetails()
        {


            JobCodeTableAdapter adapt = new JobCodeTableAdapter();
            adapt.Connection = new OleDbConnection(Program.ConnStr);//This is not mandatory.just to confirm.
            JobDetails.TblJobCodeDataTable dt = adapt.GetDataByjobcode(cmbjobcode.Text); // you can use datatable as class , but will not get all proprty
            tbljobdata.DataSource = dt;
       //     tbljobdata.Columns[17].Visible = false;
         //   MessageBox.Show(tbljobdata.Rows[0].Cells[16].Value.ToString());

                   }

        private void cmbjobcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedjobcodedetails();
        }

        private void btnCompleted_Click(object sender, EventArgs e)
        {
            getdatafromdatagrid();
        }

        private void tbljobdata_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.ColumnIndex == 0)
                tbljobdata.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            else
                tbljobdata.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }


        public void getdatafromdatagrid()
        {
           int rowno = tbljobdata.CurrentCellAddress.Y;
           if (rowno < 0)
           {
               lblstatus.Text = "Select A Row first";

           }
           else
           {
               if (rowno < tbljobdata.Rows.Count - 1)
               {                  
                   if (tbljobdata.Rows[rowno].Selected == true && tbljobdata.Rows[rowno].Cells[1].Value.ToString()!=null)
                   {
                       ArrayList newarylst = new ArrayList();
                       newarylst.Add(int.Parse(tbljobdata.Rows[rowno].Cells[16].Value.ToString()));
                       newarylst.Add(int.Parse (tbljobdata.Rows[rowno].Cells[17].Value.ToString()));
                       newarylst.Add(DateTime.Now.Date);
                       bool iscompleted = true;
                       newarylst.Add(iscompleted);
                       if (newarylst.Count != 0)
                       {
                         //  try
                         //  {
                               if (invtrnsctn.insertcompletejobcode(newarylst))
                               {
                                   MessageBox.Show("SUCESSFULLY COMPLETED " + cmbjobcode.Text);
                                   jobcomboboxload();
                               }
                               else
                               {
                                   MessageBox.Show("Unable To Complete " + cmbjobcode.Text);
                               }
                         //  }
                              
                          // catch (Exception exp)
                          // {

                           //    MessageBox.Show("There is aproblem in invoicetransaction.insertcompletejobcode(newarylst" + exp);
                            //   this.Close();
                          // }
                       }
                     
                   }
               }
           }
        }

        private void tbljobdata_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
                 
           
        
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}
